function keyLogin(){
     if (event.keyCode==13)
        addTodo();
    }  
function addTodo() {
    var str = $("#addtodo");
    if(str.val() == "" || str.val().trim().length==0){
        alert("内容不能为空");
        return;
    }                        
    var Matter = {"text":str.val(),"isdone":false};
    var list = getTodoList();
    list.push(Matter);
    saveList(list);
    showList();
    str.val("");
}  
function getTodoList(){
    var list = localStorage.getItem("todolist");
    if(list != null){
        return JSON.parse(list);
    }
    else{
        return [];
    } 
}
function saveList(list){
    localStorage.setItem("todolist",JSON.stringify(list));
}
function showList(){
    var tableUndo = $("#undo");
    var tableDone = $("#done");
    var list = localStorage.getItem("todolist");
    if(list == null){
        tableUndo.html("");
        tableDone.html("");
    }
    else{
        var undo = JSON.parse(list);
        var str1 = "";
        var str2 = "";
        for(var i = 0; i < undo.length; i++){
            if(undo[i].isdone == false){
                 str1 += "<tr><td class='inputcheckbox'><input type=\"checkbox\" onclick=\"changeStatu("+ i + ")\">"+ undo[i].text + 
                  "</td><td class='delete' onclick=\"deleteMatter("+ i + ")\">删除</td></tr>";
                tableUndo.html(str1);
            }
           else{
                str2 += "<tr><td class='inputcheckbox'><input type=\"checkbox\" checked onclick=\"changeStatu("+ i + ")\">" + undo[i].text + 
            "</td><td class='delete' onclick=\"deleteMatter("+ i + ")\">删除</td></tr>";
                tableDone.html(str2);
           }
        }
    }
}
function deleteMatter(index){
    var list = getTodoList();
    if(list.length == 1 && index == 0){
        clearList();
    }
    else{
        list.splice(index,1);
        saveList(list);
        showList();
    }
    
}
 function changeStatu(index){
    var list = getTodoList();
    var matter = list[index];
    var matter2 = {"text":matter.text,"isdone":!matter.isdone};
    list.splice(index,1,matter2);
    saveList(list);
    var tableUndo = $("#undo");
    var tableDone = $("#done");
    tableDone.html("");
    tableUndo.html(""); 
    showList();
} 
function clearList(){
    localStorage.clear();
    showList();
}
$(document).ready(function(){
    showList();
    
})
